import java.util.List;

public class MySingleList {

    //加static生成对象不依赖于外部对象
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表当前节点的头结点引用

    public void createList(int data1,int data2){
        ListNode listNode1 = new ListNode(data1);
        ListNode listNode2 = new ListNode(data2);
        listNode1.next = listNode2;
        head = listNode1;
    }

    //遍历链表
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //找关键字key
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int getSize(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //插入头结点
    public void addFirst(int data){
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    }

    //插入尾节点
    public void addLast(int data){
        ListNode listNode = new ListNode(data);
        ListNode cur = head;
        if(head != null){
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = listNode;
        }else{
            head = listNode;
        }
    }

    //任意位置插入
    public void addIndex(int data,int index) throws ListIndexOutOfException{
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == getSize()){
            addLast(data);
            return;
        }
        ListNode cur = findIndexSubOne(index);
        ListNode listNode = new ListNode(data);
        listNode.next = cur.next;
        cur.next = listNode;
    }

    //找到index-1位置的节点的地址
    private ListNode findIndexSubOne(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index -1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    private void checkIndex(int index) throws ListIndexOutOfException{
        if(index < 0 || index > getSize()){
            throw new ListIndexOutOfException("插入的下标范围异常");
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return; //一个节点都没有
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = searchPre(key);
        if(cur == null){
            System.out.println("没有这个节点");
            return;
        }
        cur.next = cur.next.next;
    }
    //找到关键字key的前一个节点
    private ListNode searchPre(int key){
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null; //没有要删除的节点
    }
    //删除所有关键字为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }
        if(head.val == key){
            head = head.next;
        }
    }
    //清空链表
    public void clear(){
        head = null;
    }

    //链表反转
    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    //从指定位置开始打印
    public void display(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //寻找中间位置
    public ListNode findMiddle(){
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null ){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //求倒数第k个节点
    public ListNode findK(int k){
        if(k <= 0 || head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
            if(fast == null){
                return null;
            }
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //判断链表是否回文结构
    public boolean chkPalindrome(){
        if(head == null){
            return false;
        }
        if(head.next == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //找中间节点
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //反转链表
        ListNode cur = slow.next; //代表当前需要翻转的节点
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while(slow != head){
            if(head.val != slow.val){
                return false;
            }
            //偶数的情况
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
    //给定一个值x，分割链表，不能改变链表元素原来的顺序
    public MySingleList.ListNode partition(int x){
        MySingleList.ListNode as = null;
        MySingleList.ListNode ae = null;
        MySingleList.ListNode bs = null;
        MySingleList.ListNode be = null;

        MySingleList.ListNode cur = head;
        while(cur != null){
            if(cur.val < x){
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }else{
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else {
                    be.next =cur;
                    be = be.next;
                }
            }
            cur = cur.next;
        }
        //有可能不会同时存在小于x 和 大于等于x 的数据
        if(as == null){
            return bs;
        }
        //第一段不为空
        ae.next = bs;
        //第二段不为空
        if(bs != null) {
            be.next = null;
        }
        return as;
    }

    //判断环
    public boolean hasCycle(){
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    //形成环
    public void createLoop(){
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = head.next;
    }
}
